/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.rpgmapper.manager;

import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector2f;
import com.jme3.texture.Image.Format;
import com.jme3.texture.Texture2D;
import com.jme3.util.BufferUtils;
import java.nio.ByteBuffer;
import org.rpgmapper.tools.AppContext;
import org.rpgmapper.tools.ImageUtils;

/**
 *
 * @author Nehon
 */
public class DrawingManager {

    private static final int MAP_SIZE = 512;
    private static final float DRAW_STEP = 0.02f;
    private Texture2D mainTexture;
    private Texture2D backupTexture;
    private boolean dirty = true;
    private int updateIn = -1;
    private static final int UPDATE_DELAY = 2;
    private Material groundMat;
    private Material dummyMat;
    private Vector2f currentPixel = new Vector2f();
    private ColorRGBA color = ColorRGBA.Black;
    private Tool currentTool = null;
    private boolean drawing;    

    public enum Tool {

        Pen, Rubber
    }

    public DrawingManager(Material groundMat, Material dummyMat) {
        this.groundMat = groundMat;
        this.dummyMat = dummyMat;
        mainTexture = new Texture2D(MAP_SIZE, MAP_SIZE, 1, Format.RGBA8);
        ByteBuffer buf = BufferUtils.createByteBuffer(MAP_SIZE * MAP_SIZE * 4);
        mainTexture.getImage().getData().add(buf);
        ImageUtils.clear(ColorRGBA.White, Format.RGBA8, mainTexture.getImage().getData(0), MAP_SIZE);
        mainTexture.getImage().setUpdateNeeded();
        groundMat.setTexture("LightMap", mainTexture);
        // activateTool(Tool.Pen);
    }

//    public void updateTexture() {
//        if (dirty) {
//
//            groundMat.setTexture("GridMap", backupTexture);
//            // ImageUtils.convert(grid, Format.RGBA8, mainTexture.getImage().getData(0));
//            mainTexture.getImage().setUpdateNeeded();
//            dummyMat.setTexture("ColorMap", mainTexture);
//            updateIn = UPDATE_DELAY;
//            dirty = false;
//        }
//    }
    public void draw(Vector2f pixel) {
        if (currentTool != null) {
            if (drawing) {
                // if (currentPixel.subtract(pixel).length() >= DRAW_STEP) {
                //ImageUtils.draw(pixel, ColorRGBA.Black, Format.RGBA8, mainTexture.getImage().getData(0), MAP_SIZE);
                if (!currentPixel.equals(pixel)) {
                    ImageUtils.drawLine(currentPixel, pixel, color, Format.RGBA8, mainTexture.getImage().getData(0), MAP_SIZE);

                    mainTexture.getImage().setUpdateNeeded();
                    groundMat.setTexture("LightMap", mainTexture);
                    //System.out.println("drwing line from" + currentPixel.mult(MAP_SIZE) + " to " + pixel.mult(MAP_SIZE));
                    currentPixel.set(pixel);
                }

                // }
            } else {
                drawing = true;
                currentPixel.set(pixel);
            }
        }

//            dummyMat.setTexture("ColorMap", mainTexture);
//            updateIn = UPDATE_DELAY;
//            dirty = false;
    }

    public void quitDrawing() {
        drawing = false;
    }

    public boolean activateTool(Tool tool) {
        switch (tool) {
            case Pen:
                if (currentTool == Tool.Pen) {
                    currentTool = null;
                    stopDrawing();
                    return false;
                } else {
                    currentTool = Tool.Pen;
                    color = ColorRGBA.Black;
                    startDrawing();
                    return true;
                }


            case Rubber:
                if (currentTool == Tool.Rubber) {
                    currentTool = null;
                    stopDrawing();
                    return false;
                } else {
                    currentTool = Tool.Rubber;
                    color = ColorRGBA.White;
                    startDrawing();
                    return true;
                }

        }
        return false;
    }

    private void stopDrawing() {

        AppContext.getRtsCam().setEnabled(true);
        AppContext.getSelectionManager().setMode(SelectionManager.Mode.Character);

    }

    private void startDrawing() {      
        AppContext.getRtsCam().setEnabled(false);
        AppContext.getSelectionManager().setMode(SelectionManager.Mode.Draw);
    }

    public void clear() {
        ImageUtils.clear(ColorRGBA.White, Format.RGBA8, mainTexture.getImage().getData(0), MAP_SIZE);
        mainTexture.getImage().setUpdateNeeded();
    }
//      private class TextureControl extends AbstractControl {
//
//        @Override
//        protected void controlUpdate(float tpf) {
//            if (updateIn > -1) {
//                if (updateIn == 0) {
//                    groundMat.setTexture("GridMap", mainTexture);
//                    ByteBuffer b1 = backupTexture.getImage().getData(0);
//                    b1.rewind();
//                    ByteBuffer b2 = mainTexture.getImage().getData(0);
//                    b2.rewind();
//                    b1.put(b2);
//                    backupTexture.getImage().setUpdateNeeded();
//                    dummyMat.setTexture("ColorMap", backupTexture);
//                    updateIn--;
//                } else {
//                    updateIn--;
//                }
//            }
//        }
//
//        @Override
//        protected void controlRender(RenderManager rm, ViewPort vp) {
//        }
//
//        public Control cloneForSpatial(Spatial spatial) {
//            return null;
//        }
//
//        public TextureControl() {
//        }
//    }
}
